package com.xinweisi.dataserver.module.drawingverification;

import com.xinweisi.dataserver.common.checkDBName.CheckDBNameService;
import com.xinweisi.dataserver.module.drawingverification.domain.dto.DrawingVerificationQueryDTO;
import com.xinweisi.dataserver.module.drawingverification.domain.entity.*;
import com.xinweisi.dataserver.module.drawingverification.domain.vo.DepositExamineVO;
import com.xinweisi.dataserver.module.memberdata.onlinemember.domain.entity.OnLineMemberEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * DrawingVerificationService
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/8/4 0004 10:56
 */
@Slf4j
@Service
public class DrawingVerificationService {
    @Autowired
    private DrawingVerificationDao drawingVerificationDao;

    @Autowired
    private CheckDBNameService checkDBNameService;

    /**
     * 打码设置
     * @param queryDTO
     * @author 
     * @date 2020/8/4 0004 11:30 
     * @return com.xinweisi.dataserver.module.drawingverification.domain.entity.DepositSettingsEntity
     */
    public DepositSettingsEntity selectDepositSettingsList(DrawingVerificationQueryDTO queryDTO) {
        DepositSettingsEntity info = new DepositSettingsEntity();
        List<DepositSettingsEntity> depositSettingsList = drawingVerificationDao.selectDepositSettingsList(queryDTO);
        if(depositSettingsList.size()>0){
            info = depositSettingsList.get(0);
        }
        return info;
    }

    /**
     * 查询审核通过的存款
     * @param queryDTO
     * @author 
     * @date 2020/8/4 0004 11:32
     * @return void
     */
    public DepositExamineVO selectDepositExamineList(DrawingVerificationQueryDTO queryDTO) {
        DepositExamineVO info = new DepositExamineVO();
        List<DepositEntity> depositExamineList = drawingVerificationDao.selectDepositExamineApplyList(queryDTO);
        double amount = 0.0;
        for (DepositEntity obj:depositExamineList) {
            queryDTO.setAmount(obj.getDepositAmount());
            DepositSettingsEntity depositSettingsEntity = selectDepositSettingsList(queryDTO);
            amount = amount + (obj.getDepositAmount() * depositSettingsEntity.getMultiple());
        }
        info.setAmount(amount);
        info.setUserId(queryDTO.getUserId());
        return info;
    }

    /**
     * 查询审核通过的提款
     * @param queryDTO
     * @author
     * @date 2020/8/4 0004 11:32
     * @return void
     */
    public ReflectEntity selectDrawingExamineList(DrawingVerificationQueryDTO queryDTO) {
        ReflectEntity info = new ReflectEntity();
        List<ReflectEntity> depositExamineList = drawingVerificationDao.selectDrawingExamineList(queryDTO);
        if(depositExamineList.size()>0){
            info = depositExamineList.get(0);
        }
        return info;
    }

    /**
     * 查询审核通过的提款
     * @param queryDTO
     * @author
     * @date 2020/8/4 0004 11:32
     * @return void
     */
    public MemberFundsFlowEntity selectMemberFundFlowList(DrawingVerificationQueryDTO queryDTO) {
        MemberFundsFlowEntity info = new MemberFundsFlowEntity();
        List<MemberFundsFlowEntity> memberFundFlowList = drawingVerificationDao.selectMemberFundFlowList(queryDTO);
        if(memberFundFlowList.size()>0){
            info = memberFundFlowList.get(0);
        }
        return info;
    }

    /**
     * 待验证的提款
     * @author 
     * @date 2020/8/4 0004 11:42 
     * @return void
     */
    public void calculationCoding() {
        List<DrawingExamineEntity> drawingExamineList = drawingVerificationDao.selectDrawingExamineApplyList();
        for (DrawingExamineEntity obj:drawingExamineList) {
            //查询存款额度
            DrawingVerificationQueryDTO queryDTO = new DrawingVerificationQueryDTO();
            queryDTO.setUserId(obj.getGameAccount());
            ReflectEntity reflectEntity = selectDrawingExamineList(queryDTO);
            String startTime = reflectEntity.getReflectTime();
            if("".equals(startTime)|| startTime==null){
                startTime = "2020-07-04 00:00:00";
            }
            String endTime = obj.getDrawingTime();
            queryDTO.setStartTime(startTime);
            queryDTO.setEndTime(endTime);
            DepositExamineVO depositExamineVO = selectDepositExamineList(queryDTO);
            //查询交易流水
//            BettingRecordQueryDTO queryDTO1 = new BettingRecordQueryDTO();
//            queryDTO1.setStartTime(startTime);
//            queryDTO1.setEndTime(endTime);
//            startTime = startTime.substring(0,startTime.indexOf(" "));
//            endTime = endTime.substring(0,endTime.indexOf(" "));
//            queryDTO1.setDBNameList(DBNameUtil.getDANameList(startTime,startTime,endTime));
//            UserEntity userEntity = userDao.getMemberListByAccount(obj.getGameAccount()).get(0);
//            queryDTO1.setGameAccount(userEntity.getAccountName());
//            BettingRecordVO bettingRecordVO = new BettingRecordVO();
//            bettingRecordVO.setGameAccount(obj.getGameAccount());
//            for (int i=0;i<queryDTO1.getDBNameList().size();i++){
//                queryDTO1.setDBName(queryDTO1.getDBNameList().get(i));
//                List<BettingRecordEntity> entityList = memberBettingRecordDao.selectBehaviorDailyList(queryDTO1);
//                for (BettingRecordEntity entity:entityList) {
//                    String balanceChange = entity.getBalanceChange();
//                    //用户资金变化
//                    String[] balances1 = balanceChange.split("_");
//                    Double fundOld = "".equals(balances1[0]) ? 0.0 : Double.parseDouble(balances1[0]);
//                    Double fundNew = "".equals(balances1[1]) ? 0.0 : Double.parseDouble(balances1[1]);
//                    double betResult = fundNew - fundOld;
//                    if (betResult < 0) {
//                        betResult = betResult * -1;
//                    }
//                    bettingRecordVO.setBetAmount(bettingRecordVO.getBetAmount()+betResult);
//                }
//            }
            MemberFundsFlowEntity memberFundsFlowEntity = selectMemberFundFlowList(queryDTO);
            //判断当前的提款是否满足条件
            DrawingExamineEntity updateVO = new DrawingExamineEntity();
            updateVO.setId(obj.getId());
            OnLineMemberEntity onLineMemberEntity = new OnLineMemberEntity();
            onLineMemberEntity.setAccount(queryDTO.getUserId());
            if(depositExamineVO.getAmount()>0){
                onLineMemberEntity.setDacodingVal(memberFundsFlowEntity.getFundsFlow());
                onLineMemberEntity.setDacodingXuVal(depositExamineVO.getAmount());
                //有存款额度  存款金额<=交易的流水
                if(depositExamineVO.getAmount()<=memberFundsFlowEntity.getFundsFlow()){
                    updateVO.setCodeStatus(1);
                    onLineMemberEntity.setRegisterTime(new Date());
                    drawingVerificationDao.updateMemberFundFlow(onLineMemberEntity);
                }else {
                    updateVO.setCodeStatus(2);
                }
            }else {
                onLineMemberEntity.setDacodingVal(memberFundsFlowEntity.getFundsFlow());
                onLineMemberEntity.setDacodingXuVal(0);
                updateVO.setCodeStatus(1);
                //无存款额度  提款金额<=交易流水
//                if(obj.getDrawingAmount()<=bettingRecordVO.getBetAmount()){
//                    updateVO.setCodeStatus(1);
//                }else {
//                    updateVO.setCodeStatus(2);
//                }
            }
            drawingVerificationDao.updateDrawingStatus(updateVO);
            drawingVerificationDao.updateMemberInfoByDacoding(onLineMemberEntity);
        }
    }
}
